//
//  SearchController.swift
//  edu
//
//  Created by karlzhou on 15/11/24.
//  Copyright © 2015年 karlzhou. All rights reserved.
//

import UIKit

class SearchController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.backgroundColor = UIColor.clearColor()
        self.setupSearchBarItem()
        setupView()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func successed(var data1:[HotSearchModel]) -> Void {
       let data = data1[0...19]
        print("self.view: \(self.view.frame.size.width)")
        var  row = 0
        var column = 0
       
        var orignWidth:CGFloat = 10
        for var j = 0; j < data.count; ++j {
            let words =   data[j].words!
            let word: NSString = words as NSString
            var titleSize = word.sizeWithAttributes([NSFontAttributeName:UIFont.systemFontOfSize(18.0)])
            titleSize.width+=20
            let button =  UIButton(type: .System)
            
           
            ++column
            // print("originWidth: \(orignWidth)")
            if((orignWidth + titleSize.width)  > self.view.frame.size.width ){
                orignWidth = 10
                ++row
                column = 1
              
                //print(level)
            }
            //let navigationBarHeight = self.navigationController?.navigationBar.frame.size.height
            button.frame = CGRectMake(orignWidth, CGFloat(row*40 + 80), titleSize.width, 30)
            
            orignWidth += titleSize.width + CGFloat(20)
            print(button.frame)
           
            button.titleLabel?.textColor = UIColor.blackColor()
            //button.titleLabel?.font = UIFont.boldSystemFontOfSize(18.0)
            button.tintColor = UIColor.grayColor()
            button.setBackgroundImage(UIImage(named: "btn_player_view_small"), forState: .Normal)
            button.setBackgroundImage(UIImage(named: "btn_player_view_small_pressed"), forState: .Highlighted)
            button.titleLabel?.adjustsFontSizeToFitWidth = true
            
            button.setTitle(words, forState: .Normal)
             //button.sizeToFit()
           // button.sizeThatFits(CGSize(width: 60, height: 30))
            self.view.addSubview(button)
            
        }
        
        
        
    }
    
    func failure (code:Int,message:String){
        
        let subView = UILabel(frame: CGRectMake(200,200,300,100))
        subView.text = message
        self.view.addSubview(subView)
        
    }
    func setupView(){
        //let successed:
        self.view.backgroundColor = UIColor.whiteColor()
        APIClient.getHotSearch(successed, failure: failure)
        
    }
    
    func setupSearchBarItem(){
        let searchBar =  UISearchBar(frame: CGRectMake(0,0,self.view.frame.size.width-100,20))
        //searchBar.text = "请输入你感兴趣的课程"
        searchBar.placeholder = "请输入你感兴趣的课程"
        self.navigationItem.titleView = searchBar
        
    }
    
    
}
